package com.hubiao.pay.merchant.controller;

import com.hubiao.pay.merchant.api.MerchantService;
import com.hubiao.pay.merchant.api.dto.MerchantDTO;
import com.hubiao.pay.merchant.common.util.SecurityUtil;
import com.hubiao.pay.merchant.service.FileService;
import com.hubiao.pay.merchant.service.SmsService;
import com.hubiao.pay.merchant.vo.MerchantRegisterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Description
 *
 * @author hubiao
 * @since 2020-11-01 21:51
 */
@Api(description = "商户平台应用接口")
@RestController
public class MerchantController {

    /**
     * 注入的远程的调用接口
     */
    @org.apache.dubbo.config.annotation.Reference
    private MerchantService merchantService;

    /**
     * 注入本地的bean
     */
    @Resource
    private SmsService smsService;

    @Resource
    private FileService fileService;


    /**
     * 根据id获取商户信息
     * @param id
     * @return
     */
    @ApiOperation("根据商户id获取商户信息")
    @GetMapping("/merchants/{id}")
    public MerchantDTO queryMerchantById(@PathVariable("id") Long id) {
        return merchantService.queryMerchantById( id );
    }

    @ApiOperation("获取手机验证码")
    @GetMapping("/smsCode")
    @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String", paramType = "query", required = true)
    public String getSMSCode(String phone) {
        return smsService.sendMsg( phone );
    }


    @ApiOperation(value = "商户注册Api", httpMethod = "POST")
    @ApiImplicitParam(name = "merchantRegisterVO", value = "商户注册信息", required = true, paramType = "body", dataType = "MerchantRegisterVO")
    @PostMapping("/merchant/register")
    public MerchantRegisterVO register(@RequestBody MerchantRegisterVO merchantRegisterVO) {
        //1.调用本地方法校验验证
        smsService.checkVerifyCode( merchantRegisterVO.getVerifyCode(), merchantRegisterVO.getVerifyKey() );
        //2.调用dubbo接口保存商户注册信息
        MerchantDTO merchantDTO = new MerchantDTO();
        merchantDTO.setMerchantName( merchantRegisterVO.getUserName() );
        merchantDTO.setMobile( merchantRegisterVO.getMobilePhone() );
        merchantService.createMerchant( merchantDTO );
        return merchantRegisterVO;
    }

    @ApiOperation(value = "使用七牛云上传附件", httpMethod = "POST")
    @PostMapping("/upload")
    public String upload(@ApiParam(value = "上传得文件", required = true) @RequestParam("file") MultipartFile multipartFile) throws IOException {
        //调用本地fileService进行文件上传
        return fileService.upload( multipartFile );
    }


    @ApiOperation(value = "资质申请", httpMethod = "POST")
    @PostMapping("/my/merchant/save")
    public void saveMerchant(@RequestBody MerchantDTO merchantDTO) {
        //1.解析token，获取当前登录的商户id
        Long merchantId = SecurityUtil.getMerchantId();
        //2.远程调用dubbo服务的merchantService
        merchantService.applyQualifications( merchantId, merchantDTO );
    }

}
